package com.hxkcloud.nurse.activitys;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.popup.DateSelectorPopWindow;
import com.hxkcloud.nurse.utils.BrightnessUtils;
import com.hxkcloud.nurse.utils.StringUtil;

import java.util.Calendar;

/**
 * Created by william_sun on 2016-07-06.
 * Description:注册用户个人信息填写(姓名性别年龄)
 */
public class UserBasicInfoActivity extends BaseActivity implements View.OnClickListener {

    private LinearLayout ll_title;
    private ImageView iv_back;
    private TextView tv_title;
    private EditText et_name;
    private EditText et_sex;
    private EditText et_age;
    private Button bt_nextstep;

    private boolean nameInput = false;
    private boolean sexInput = false;
    private boolean ageInput = false;

    private PopupWindow popup_sex;//选择性别的popup
    private Button bt_male;
    private Button bt_female;
    private Button bt_cancel;

    private DateSelectorPopWindow dateSelect;

    private String tel = "";
    private String password = "";
    private String birthDay = "";

    @Override
    protected void updateByHandler(Message msg) {
        switch (msg.what) {

        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_userbasicinfo);
        if (getIntent().hasExtra("tel")) {
            tel = getIntent().getStringExtra("tel");
        }
        if (getIntent().hasExtra("password")) {
            password = getIntent().getStringExtra("password");
        }
        initView();
        initPopup();
    }

    @Override
    protected void initView() {
        ll_title = (LinearLayout) findViewById(R.id.ll_title);
        iv_back = (ImageView) findViewById(R.id.iv_title_back);
        tv_title = (TextView) findViewById(R.id.tv_title_title);
        et_name = (EditText) findViewById(R.id.et_userbasicinfo_name);
        et_sex = (EditText) findViewById(R.id.et_userbasicinfo_sex);
        et_age = (EditText) findViewById(R.id.et_userbasicinfo_age);
        bt_nextstep = (Button) findViewById(R.id.bt_userbasicinfo_nextstep);

        setTitlePadding(ll_title);
        iv_back.setOnClickListener(this);
        iv_back.setVisibility(View.VISIBLE);
        tv_title.setText(getResources().getString(R.string.personalinfo));

//http://www.cnblogs.com/kakafra/archive/2012/09/20/2696154.html
        et_sex.setFocusable(false);
//        et_age.setFocusable(false);
        et_name.setOnClickListener(this);
        et_sex.setOnClickListener(this);
        et_age.setOnClickListener(this);
        bt_nextstep.setOnClickListener(this);

        et_name.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (StringUtil.isEmpty(s.toString())) {
                    nameInput = false;
                    bt_nextstep.setBackgroundResource(R.drawable.bt_grey);
                    bt_nextstep.setEnabled(false);
                } else {
                    nameInput = true;
                    if (sexInput && ageInput) {
                        bt_nextstep.setBackgroundResource(R.drawable.bg_btn_lightgreen_corner);
                        bt_nextstep.setEnabled(true);
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        et_age.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (StringUtil.isEmpty(s.toString())) {
                    ageInput = false;
                    bt_nextstep.setBackgroundResource(R.drawable.bt_grey);
                    bt_nextstep.setEnabled(false);
                } else {
                    ageInput = true;
                    if (nameInput && sexInput) {
                        bt_nextstep.setBackgroundResource(R.drawable.bg_btn_lightgreen_corner);
                        bt_nextstep.setEnabled(true);
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    private void initPopup() {
        popup_sex = new PopupWindow(this);
        View viewSex = LayoutInflater.from(this).inflate(R.layout.popup_selectsex, null);
        popup_sex.setContentView(viewSex);

        popup_sex.setWidth(screenWidth);
        popup_sex.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        popup_sex.setFocusable(true);
        popup_sex.setTouchable(true);
        popup_sex.setOutsideTouchable(true);
        popup_sex.setBackgroundDrawable(new ColorDrawable(0x00000000));
        popup_sex.setAnimationStyle(R.style.popupwindow_anim_default);

        bt_male = (Button) viewSex.findViewById(R.id.bt_selectsex_male);
        bt_female = (Button) viewSex.findViewById(R.id.bt_selectsex_female);
        bt_cancel = (Button) viewSex.findViewById(R.id.bt_selectsex_cancel);

        bt_male.setOnClickListener(this);
        bt_female.setOnClickListener(this);
        bt_cancel.setOnClickListener(this);

        popup_sex.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                BrightnessUtils.lightOn(UserBasicInfoActivity.this);
            }
        });
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_title_back:
                finish();
                break;
            case R.id.et_userbasicinfo_name:
                et_name.setCursorVisible(true);
                break;
            case R.id.et_userbasicinfo_sex:
                closeInputMethodManager();
                et_name.setCursorVisible(false);
                popup_sex.showAtLocation(ll_title, Gravity.BOTTOM, 0, 0);
                BrightnessUtils.lightOff(UserBasicInfoActivity.this);
                break;
            case R.id.et_userbasicinfo_age:
                et_age.setCursorVisible(true);
//                closeInputMethodManager();
//                et_name.setCursorVisible(false);
//                if (dateSelect == null) {
//                    dateSelect = new DateSelectorPopWindow(this);
//                    dateSelect.setTimeListener(new DateSelectorPopWindow.TimeListener() {
//                        @Override
//                        public void setTime(String time) {
//                            birthDay = time.replace(".", "-");
//                            int year = Integer.valueOf(time.split("\\.")[0]);
//                            Calendar c = Calendar.getInstance();
//                            int curYear = c.get(Calendar.YEAR);
//                            int old = curYear - year;
//                            if(old==0){
//                                et_age.setText("<1");
//                            }else{
//                                et_age.setText(old + "");
//                            }
//
//                            ageInput = true;
//                            if (sexInput && nameInput) {
//                                bt_nextstep.setBackgroundResource(R.drawable.bg_btn_lightgreen_corner);
//                                bt_nextstep.setEnabled(true);
//                            }
//                        }
//                    });
//                }
//                dateSelect.show(ll_title);
                break;
            case R.id.bt_userbasicinfo_nextstep:
                String name = et_name.getText().toString();
                String sex = et_sex.getText().toString();
                int age = Integer.valueOf(et_age.getText().toString());
                int birthYear = Calendar.getInstance().get(Calendar.YEAR) - age;
                birthDay = birthYear + "-01-01";

                Intent iAddress = new Intent(mContext, UserInfoActivity.class);
                iAddress.putExtra("tel", tel);
                iAddress.putExtra("password", password);
                iAddress.putExtra("name", name);
                iAddress.putExtra("sex", sex);
                iAddress.putExtra("age", birthDay);
                startActivity(iAddress);
                break;
            case R.id.bt_selectsex_male:
                et_sex.setText(getResources().getText(R.string.male));
                popup_sex.dismiss();
                sexInput = true;
                if (nameInput && ageInput) {
                    bt_nextstep.setBackgroundResource(R.drawable.bg_btn_lightgreen_corner);
                    bt_nextstep.setEnabled(true);
                }
                break;
            case R.id.bt_selectsex_female:
                et_sex.setText(getResources().getText(R.string.female));
                popup_sex.dismiss();
                sexInput = true;
                if (nameInput && ageInput) {
                    bt_nextstep.setBackgroundResource(R.drawable.bg_btn_lightgreen_corner);
                    bt_nextstep.setEnabled(true);
                }
                break;
            case R.id.bt_selectsex_cancel:
                popup_sex.dismiss();
                break;
        }
    }
}
